package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 别名
 */
public class JDBCDemo5 {
    public static void main(String[] args) {
        try (
                Connection connection = DBUtil.getConnection();
        ){
            Statement statement = connection.createStatement();
            /*
            统计1年级1班共有多少人：
            SELECT COUNT(*) count
            FROM student s
            JOIN class c ON s.class_id=c.id
            WHERE c.name='1年级1班'
             */
            String sql = "SELECT COUNT(*) count " +
                         "            FROM student s " +
                         "            JOIN class c ON s.class_id=c.id " +
                         "            WHERE c.name='1年级1班'";
            ResultSet resultSet = statement.executeQuery(sql);
            if (resultSet.next()){
                int count = resultSet.getInt("count");
                System.out.println("一年级一班一共"+count+"人");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
